package com.douqu.game.battle.entity;

import com.douqu.game.battle.server.GameServer;
import com.douqu.game.core.controller.MonsterController;
import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.entity.Level;
import com.douqu.game.core.entity.battle.BattleAObject;
import com.douqu.game.core.entity.battle.BattleSkill;
import com.douqu.game.core.controller.SpriteController;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.entity.sprite.Player;
import com.douqu.game.core.entity.sprite.PlayerLv;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 主将
 * Created by bean on 2017/7/27.
 */
public class BattleMasterAObject extends BattleAObject {

    Logger logger = Logger.getLogger(BattleMasterAObject.class);

    public BattleMasterAObject(SpriteController spriteController, int uniqueId)
    {
        super(spriteController, uniqueId);

        PlayerLv playerLv = null;
        if(spriteController instanceof PlayerController)
        {
            Player player = ((PlayerController) spriteController).getPlayer();
            playerLv = DataFactory.getInstance().getGameObject(DataFactory.PLAYER_LV_KEY, player.getLv());
        }
        else if(spriteController instanceof MonsterController)
        {
            Level level = ((MonsterController) spriteController).getLevel();
            playerLv = DataFactory.getInstance().getGameObject(DataFactory.PLAYER_LV_KEY, level.master);
        }

        if(playerLv != null)
        {
            this.objectId = playerLv.id;
            this.hp = maxHp  = playerLv.hp;
            this.physicsAtk = playerLv.physicsAtk;
            this.punctureAtk = playerLv.punctureAtk;
            this.magicAtk = playerLv.magicAtk;
            this.physicsDef = playerLv.physicsDef;
            this.punctureDef = playerLv.punctureDef;
            this.magicDef = playerLv.magicDef;
            this.power = playerLv.power;
            this.agility = playerLv.agility;
            this.wisdom = playerLv.wisdom;

            this.defaultSkill = new BattleSkill(spriteController, this, playerLv.defaultSkill);
            this.cdSkills = new BattleSkill[playerLv.cdSkills.length];
            for(int i = 0; i < cdSkills.length; i++)
            {
                cdSkills[i] = new BattleSkill(spriteController, this, playerLv.cdSkills[i]);
            }
        }

        this.unitType = ConstantFactory.UNIT_TYPE_LAND;
    }


    @Override
    public int getAtkRange()
    {
        return 1;
    }

    public BattleSkill getReleaseSkill(long currentTime)
    {
        for(BattleSkill skill : cdSkills)
        {
            if(skill.isCanRelease(currentTime))
                return skill;
        }
        return null;
    }

    @Override
    public TwoTuple<List<BattleAObject>, BattleAObject> checkAtkTarget(List<BattleAObject> targetList)
    {
        BattleAObject atkTarget = null;
        List<BattleAObject> result = new CopyOnWriteArrayList<>();
        for(BattleAObject battleAObject : targetList)
        {
            atkTarget = defaultSkill.isInAtkRange(this, battleAObject) ? battleAObject : null;
            if(atkTarget != null)
            {
                result.add(atkTarget);
                if(defaultSkill.isSingleTarget())
                    break;
            }
        }

        return new TwoTuple<>(result, null);
    }

    @Override
    public void checkDefaultSkillTarget(long currentTime)
    {
        if(!defaultSkill.isCheckTarget())
            return;

        if(!defaultSkill.isCanRelease(currentTime))
            return;

        checkDefaultAtk();

        //是否还在攻击范围内
        BattleAObject firstTarget = defaultSkill.getFirstTarget();
        if(firstTarget != null)
        {
            if(defaultSkill.isInAtkRange(this, firstTarget))
            {
                defaultSkill.release(defaultSkill.getTargets(), firstTarget.getPosition(), GameServer.currentTime);

                checkDefaultAtk();
                return;
            }
        }


        TwoTuple<List<BattleAObject>,BattleAObject> targets = checkAtkTarget(spriteController.getAttachment().getTargetBattleTmp().getSoldierList());
        if(targets.getFirst() != null && targets.getFirst().size() > 0)
        {
//                addAtkTarget(targets.getFirst());
            addDefaultAtk(targets.getFirst());

            battleParentController.sendSoldierBattleStart(this, targets.getFirst().get(0));

            defaultSkill.release(targets.getFirst(), targets.getFirst().get(0).getPosition(), GameServer.currentTime);

            checkDefaultAtk();
        }
    }



//    @Override
//    public boolean checkDefaultSkillCurAtkTargetOK(long currentTime)
//    {
//        if(!defaultSkill.isHaveTarget())
//            return false;
//
//        BattleAObject atkTarget = defaultSkill.getFirstTarget();
//        if(atkTarget == null)
//            return false;
//
//        //是否还在攻击范围内
//        if(defaultSkill.skill.isInAtkRange(this, atkTarget))
//        {
//            if(defaultSkill.isCanRelease(currentTime))
//            {
//                defaultSkill.release(atkTarget, atkTarget.getPosition(), GameServer.currentTime);
//
//                checkDefaultAtk();
//            }
//        }
//        return true;
//    }


    @Override
    public String getName()
    {
        return "Master_" + teamNo;
    }



}
